package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.House;
import com.hzdl.house_mgr.service.HouseService;
import com.hzdl.house_mgr.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @RequestMapping("findAll")
    @ResponseBody
    public AjaxResult house(House house,Integer page,Integer limit){
        PageInfo<House> all = houseService.findAllByWhere(house,page, limit);
        int count = houseService.getCountByWhere(house);
        return AjaxResult.ok(count,all.getList());
    }

    @RequestMapping("findByWhere")
    public String findByHouse(String community, Model model) {

        if ( community != null) {
            List<House> houses = houseService.findByWhere(community);
            int count = houseService.getCountByWhere1(community);
            List<House> all = houseService.findAll();
            model.addAttribute("houseList", houses);
            model.addAttribute("count", count);
            model.addAttribute("houses", all);
            return "client/rentalSearch";
        }else {
            List<House> all = houseService.findAll();
            model.addAttribute("houses", all);
            return "client/rentalSearchAll";
        }

    }


    @RequestMapping("addOrUpdateHouse")
    @ResponseBody
    public AjaxResult addOrUpdateHouse(House house,String hidHouseId){
        int i =0;
        if (hidHouseId != null && hidHouseId.length() !=0){
            i = houseService.updateHouse(house);
            if (i>0){
                return AjaxResult.UPD_SUCCESS();
            }
            return AjaxResult.UPD_FAILED();
        }else {
            i=houseService.addHouse(house);
            if (i>0){
                return  AjaxResult.ADD_SUCCESS();
            }
            return AjaxResult.ADD_FAILED();

        }
    }

    @RequestMapping("deleteHouse")
    @ResponseBody
    public AjaxResult deleteHouse(Integer houseId){
        int i = houseService.deleteHouse(houseId);
        if (i>0){
            return AjaxResult.DEL_SUCCESS();
        }
        return AjaxResult.DEL_FAILED();
    }

    @RequestMapping("delAllHouse")
    @ResponseBody
    public AjaxResult delAllHouse(String houseIds){
        String[] houseId = houseIds.split(",");
        int i = houseService.deleteAllHouse(houseId);
        if (i>0 && i == houseId.length){
            return AjaxResult.DELSOME_SUCCESS();
        }
        return  AjaxResult.DELSOME_FAILED();
    }

    /*@RequestMapping("lookHouse")
    public String lookHouse(Integer houseId, HttpServletRequest req ){
        House id= houseService.findById(houseId);
        if (id != null){
            req.setAttribute("houseId",id);
            return  "client/lookHouse";
        }

      return "";
    }*/

    @RequestMapping("findHouse")
    public String findHouse(Model model,Integer houseId){
        House house = houseService.findHouse(houseId);
        if(house!=null){
            model.addAttribute("house",house);
            return "client/order";
        }else {
            return "";
        }
    }

    //发布房源
    @RequestMapping("pushHouse")
    @ResponseBody
    public AjaxResult pushHouse(House house){
        int i = houseService.pushHouse(house);
        if(i>0){
            return  AjaxResult.ADD_SUCCESS();
        }else {
            return  AjaxResult.ADD_FAILED();
        }
    }
}
